<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加试听课程：</title>
    <link rel="stylesheet" type="text/css" href="../../plugins/layui/css/layui.css" />
    <link href="../../kitAdmin/css/app.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../plugins/select2/dist/css/select2.min.css"/>
    <style type="text/css">
        /* .select2-container{
            display:none;
        } */

        .layui-unselect.layui-form-select {
            display: none;
        }
    </style>
</head>

<body>
<div class='kit-table'>
    <div style='margin:10px'>

        <form class='layui-form' action='' lay-filter='form-edit'>

            <!--<div class='layui-inline'>-->
                <!--<label class='layui-form-label'>选择老师</label>-->
                <!--<select  placeholder=''data-link='{:teacherId}' id='_teacherId' name='teacherId' class='layui-input'>-->
                    <!--<option value='请选择' selected="true">请选择</option>-->
                <!--</select>-->
            <!--</div>-->
            <div class='layui-inline'>
                <label class='layui-form-label'>选择学生</label>
                <select data-link='{:studentId}' id='_studentId' name='studentId' class='layui-input' style="width: 280px!important; ">
                    <option value='请选择' selected="true">请选择</option>
                </select>
            </div>
            <br>
            <!--<div class='layui-inline'>-->
                <!--<label class='layui-form-label'>课程</label>-->
                <!--<select lay-filter="_cource" data-link='{:cource}' id='_cource' name='cource' class='layui-input'>-->
                    <!--<option value='请选择' selected="true">请选择</option>-->
                <!--</select>-->
            <!--</div>-->
            <div class='layui-inline' style="margin-top:20px">
                <label class='layui-form-label'>购买日期</label>
                <div class='layui-input-inline'>
                    <input type='text' placeholder='' data-link='{:purchaseDate}' id='_purchaseDate' name='purchaseDate' class='layui-input'>
                    <!--<input type='text' placeholder='' data-link='{:purchaseDate}' id='_purchaseDate' name='purchaseDate' >-->
                    <!--<div class='layui-block'>-->
                        <!--<input type="text" class="layui-input" data-link='{:purchaseDate}' autocomplete="off" id='_purchaseDate' name='purchaseDate' placeholder="选择时间" />-->
                    <!--</div>-->
                </div>
            </div>
            <br>


            <div class='layui-form-item' style="margin-top:20px">
                <div class='layui-inline'>
                    <label class='layui-form-label'>购买时长</label>
                    <input type='hidden' placeholder='' data-link='{:hoursEnrolled}' id='_hoursEnrolled' name='hoursEnrolled' class='layui-input'>

                    <!--<div class='layui-input-inline'>
                        <input type='text' placeholder='' data-link='{:hoursEnrolled}' id='_hoursEnrolled' name='hoursEnrolled' class='layui-input'>
                    </div>-->
                    <div class="layui-input-inline" style="width: 80px;">
                        <input type="text" data-link='{:hours}' id='_hours' placeholder="小时" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">小时</div>
                    <div class="layui-input-inline" style="width: 80px;">
                        <input type="text" data-link='{:minutes}' id='_minutes' placeholder="分钟" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">分钟</div>
                </div>
                <div class='layui-inline'>
                    <label class='layui-form-label'>购买金额</label>
                    <div class='layui-input-inline'>
                        <input type='text' placeholder='' data-link='{:money}' id='_money' name='money' class='layui-input'>
                    </div>
                </div>
            </div>
            <br>
            <!--<div class='layui-inline'>-->
                <!--<label class='layui-form-label'>课程等级</label>-->
                <!--<select lay-filter="_courceLevel" data-link='{:courceLevel}' id='_courceLevel' name='courceLevel' class='layui-input'>-->
                    <!--<option value='请选择' selected="true">请选择</option>-->
                <!--</select>-->
            <!--</div>-->
            <div class='layui-inline'>
                <label class='layui-form-label'>负责人</label>
                <select type='text' data-link='{:sellerId}' id='_sellerId' name='sellerId' class='layui-input'>
                    <option value='请选择' selected="true">请选择</option>
                </select>
            </div>
            <br>

            <div class='layui-inline' style="margin-top:6px">
                <label class='layui-form-label'>付款方式</label>
                <div class="layui-input-block">
                    <input type="radio" id="taobao" name="payMethod" value="淘宝付款" title="淘宝付款" checked>
                    <input type="radio" id ="weixin" name="payMethod" value="微信付款" title="微信付款">
                    <input type="radio" id ="zhuan" name="payMethod" value="转学生" title="转学生">
                    <input type="radio" id="hui" name="payMethod" value="恢复上课" title="恢复上课">
                    <input type="radio" id="qita" name="payMethod" value="其他" title="其他">
                </div>
            </div>
            <br>


            <div class='layui-inline' style="margin-top:6px">
                <label class='layui-form-label'>活动方式</label>
                <div class="layui-input-block">
                    <input type="radio" id="putong" name="eventsWay" value="普通" title="普通" checked>
                    <input type="radio" id="tuangou" name="eventsWay" value="团购" title="团购">
                </div>
            </div>
            <br>

            <div class='layui-inline' style="margin-top:6px">
                <label class='layui-form-label'>课程类型</label>
                <div class="layui-input-block">
                    <input type="radio" id="xinbao" name="type" value="新报课程" title="新报课程" checked>
                    <input type="radio" id ="xubao" name="type" value="续报课程" title="续报课程">
                    <input type="radio" id="buke" name="type" value="补课时" title="补课时">
                </div>
            </div>

            <div class="layui-form-item layui-form-text" style="margin-top:6px;width: 60%">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea id='_remark' name='remark' placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>

            <!--<div class='layui-inline'>-->
                <!--<label class='layui-form-label'>试听类型</label>-->
                <!--<div class='layui-input-block'>-->
                    <!--&lt;!&ndash; <input type='text' placeholder=''data-link='{:homeworkLike}' id='_homeworkLike' name='homeworkLike' class='layui-input'> &ndash;&gt;-->
                    <!--<input type="radio" data-link='{:trylistenType}' name="trylistenType" value="0" title="新试听"/>-->
                    <!--<input type="radio" data-link='{:trylistenType}' name="trylistenType" value="1" title="二次试听"/>-->
                <!--</div>-->
            <!--</div>-->
            <!--<div class='layui-inline'>-->
                <!--<label class="layui-form-label">试听时间</label>-->
                <!--<div class="layui-input-inline" style="margin-right: 0px; width: 146px;">-->
                    <!--<input type='text' placeholder=''data-link='{:auditionTime}' id='_auditionTime' name='auditionTime' class='layui-input'>-->
                <!--</div>-->
                <!--<span class="input-group-btn">-->
			        <!--<a style="height: 38px;line-height:38px;" class="layui-btn layui-btn-sm" type="button" onclick="setAuditionTime()">添加</a>-->
		      	<!--</span>-->
            <!--</div>-->
            <!--<div class='layui-inline'>-->
                <!--<label class="layui-form-label">预留时间</label>-->
                <!--<div class="layui-input-inline" style="margin-right: 0px; width: 146px;">-->
                    <!--<input type='text' placeholder=''data-link='{:preTime}' id='_preTime' name='preTime' class='layui-input'>-->
                <!--</div>-->
                <!--<span class="input-group-btn">-->
			        <!--<a style="height: 38px;line-height:38px;" class="layui-btn layui-btn-sm" type="button" onclick="setRetentionTime()">添加</a>-->
		      	<!--</span>-->
            <!--</div>-->
        </form>
    </div>
</div>
<script src="../../common/js/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="../../static/js/base.js" type="text/javascript" charset="utf-8"></script>
<script src="../../plugins/jsviews/jsviews.js" type="text/javascript"></script>
<script src="../../plugins/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="../../plugins/select2/dist/js/select2.min.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">
    // 			var uid = getQueryVariable('studentId');
    var coursetemp = "1";
    var entity;
    var purchaseCourseId = getQueryVariable('uniqueid');

    var callback_save=function(){
        console.log($('.layui-form').serialize());

        var hours=0;
        var minutes=0;
        if($('#_hours').val()){
            hours=$('#_hours').val();
        }
        if($('#_minutes').val()){
            minutes=$('#_minutes').val();
        }
        var total=parseInt(hours*60)+parseInt(minutes);
        $('#_hoursEnrolled').val(total);

        return $('.layui-form').serialize();
    }

    // 			function bindFormData(){
    // 				if(uid){
    // 					$.ajax({
    // 						url:'/students/'+uid,
    // 						type:'get',
    // 						//async:false,
    // 						success:function(data){
    // 							entity=data.data;
    // 							bindEnglishAbility();
    // 							bindLessonLevelSelect2();
    // 							$.link(true, ".kit-table", data.data);
    // 							$('select').select2().change();
    // 							//console.log(data.data.istrylisten);

    // 							$('input[name="istrylisten"]').val("是");
    // 						},
    // 						error:function(){
    // 							parent.layer.alert('系统或网络错误！');
    // 						}
    // 					});
    // 				}
    // 			}


    $(document).ready(function(){
        $("#_purchaseDate").val(new Date().Format("yyyy-MM-dd"));


        // $('.datepicker').datepicker({
        //     format: 'yyyy-mm-dd',
        //     startDate: '-3d'
        // })

// 				bindFormData();
// 				bindSexSelect2();

        bindTeacherSelect2();
// 				bindRecommendSelect2();
        bindFuzerenSelect2();
        bindLessonSelect2();
        bindLessonLevelSelect2();
        bindStudentSelect2();
        // 2018-10-5 chenbin: 新增，绑定学生

        if (purchaseCourseId != null && purchaseCourseId.length != 0) {
            purchaseCourseViews(purchaseCourseId);
        }


        //select2通过模板绑定值以后不会自动刷新，需手动刷新一下；
        //注意，layui的select和select2用的不是一个控件，如果使用layui的select下面的这句可以注释掉
        //如果是select2，这下面的需要保留
        $('select').select2().change();

        $('#_cource').on("select2:select",function(){
            $('#_courceLevel').empty();
            $.ajax({
                url : baseUrl+"/levels/select2/" + $(this).val(),
                async : false,
                dataType : 'json',
                success : function(result) {
                    $('#_courceLevel').select2({
                        data : result
                    });
                }
            });
        });


        layui.use(['form'], function(){
            /* var form = layui.form;
            form.on('submit',function(){

            }) */
        });

        $("select#_studentId").change(function(){
            var stid = $(this).val();
            $.ajax({
                type: "get",
                url:baseUrl+ '/students/' + stid,
                async: true,
                success: function(data) {
                    if(data.data!=null && data.data.fuzerenId!=null){
                        // alert(data.data.fuzerenId);
                        select2ByValue(data.data.fuzerenId);
                        // $("#_fuzerenId").select2().select2('val', 'zhuyouhua')
                        // $("#_fuzerenId").select2('val','zhuyouhua')

                        // $("#_fuzerenId").val(data.data.fuzerenId);
                        // $("#_fuzerenId").val("FC3CA94F01884F1ABA90950CBA5F4F84");
                    }else {
                        select2ByValue('请选择');
                    }
                }
            });
        });

        // $("#_purchaseDate").val(new Date(value).Format("yyyy-MM-dd"));

    });

    // 通过id获取select2的value值
    function getSelect2Value(obj) {
        var select2Obj = $jq('#'+obj).select2();
        return select2Obj.select2("val");
    }

    // 通过id获取select2的text值，这里的text值可能有空格，需注意
    function getSelect2Text(obj) {
        var select2Obj = $jq('#'+obj).select2();
        return select2Obj.find("option:selected").text();
    }

    // 通过text 设置select2的默认值
    function select2ByText (obj, text) {
        var select2Obj = $jq('#'+obj).select2();
        $(obj).select("option").each(function(data){
            if (text == data.text.strip()) {
                select2Obj.val(data.value).trigger("change");
            }
        });
    }

    // 通过value 设置select2的默认值
    function select2ByValue (value) {
        var select2Obj = $("#_sellerId").select2();
        select2Obj.val(value).trigger("change");
    }

    // 通过value 设置select2的默认值
    function select2ByValueStudentId (value) {
        var select2Obj = $("#_studentId").select2();
        select2Obj.val(value).trigger("change");
    }


    function getSelectData(){
        console.log(getSelect2Value("mul-itdragon"));
        console.log(getSelect2Text("itdragon"));
        var mulItdragonVal = $jq("#mul-itdragon").select2("val");
        if (null == mulItdragonVal) {
            console.log("Over !");
            return ;
        }
        console.log(mulItdragonVal);
        var mulItdragonData = $jq("#mul-itdragon").select2('data');
        mulItdragonData.each(function(data){
            console.log("value : ", data.id);
            console.log("text : ", data.text);
        });

    }

    function bindLessonLevelSelect2(data) {
        //console.log(entity);
        var courceData = [];
        $.ajax({
            type : "get",
            url : baseUrl+'/levels/select2/'+coursetemp,
            async : false,
            success : function(data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id : '请选择',
                    text : '请选择'
                });
            }
        });

        $('#_courceLevel').select2({
            data : courceData
        });
    }

    function bindLessonSelect2() {
        var courceData = [];
        $.ajax({
            type : "get",
            url : baseUrl+'/lessons/select2',
            async : false,
            success : function(data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id : '请选择',
                    text : '请选择'
                });
            }
        });

        $('#_cource').select2({
            data : courceData
        });

    }

    function bindEnglishAbility() {
        var functional = entity.englishAbility;
        var groupCheckbox = $("input[name='englishAbility']");
        for (i = 0; i < groupCheckbox.length; i++) {
            var val = groupCheckbox[i].value;
            if (functional.split(',').indexOf(val) != -1) {
                groupCheckbox[i].checked = true;
            }
        }
    }

    /* function bindSexSelect2() {
        var sexData = [ {
            id : '请选择',
            text : '请选择'
        }, {
            id : '男',
            text : '男'
        }, {
            id : '女',
            text : '女'
        } ];

        $('#sex').select2({
            data : sexData
        });
    } */

    /* function bindRecommendSelect2() {
        var courceData = [];
        $.ajax({
            type : "get",
            url : '/students/select2',
            async : false,
            success : function(data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id : '请选择',
                    text : '请选择'
                });
            }
        });

        $('#_recommend').select2({
            data : courceData
        });
    } */

    // 2018-10-5 chenbin: 新增，绑定学生
    function bindStudentSelect2() {
        var courceData = [];
        $.ajax({
            type : "get",
            url :baseUrl+ '/students/select3',
            async : false,
            success : function(data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id : '请选择',
                    text : '请选择'
                });
            }
        });

        $('#_studentId').select2({
            data : courceData
        });
    }

    function bindFuzerenSelect2() {
        var courceData = [];
        $.ajax({
            type : "get",
            url :baseUrl+ '/admins/select2',
            async : false,
            success : function(data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id : '请选择',
                    text : '请选择'
                });
            }
        });

        $('#_sellerId').select2({
            data : courceData
        });
    }
    function bindTeacherSelect2() {
        var courceData = [];
        $.ajax({
            type : "get",
            url : baseUrl+'/teachers/select2',
            async : false,
            success : function(data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id : '请选择',
                    text : '请选择'
                });
            }
        });

        $('#_teacherId').select2({
            data : courceData
        });
    }

    function purchaseCourseViews(purchaseCourseId) {
        $.ajax({
            url: baseUrl + '/purchaseCourseViews/id',
            data:{
                uniqueid:purchaseCourseId
            },
            type: 'get',
            async: false,
            success: function(data) {
                // var htm = "";
                // data = data.rows;
                // $.link(true, ".kit-table", data.data);

                if(data.data.eventsWay!=null){
                    if(data.data.eventsWay=='普通'){
                        $("#putong").attr("checked", true)
                    }else{
                        $("#tuangou").attr("checked", true)
                    }
                }

                if(data.data.type!=null){
                    if(data.data.type=='新报课程'){
                        $("#xinbao").attr("checked", true)
                    }else if(data.data.type=='续报课程'){
                        $("#xubao").attr("checked", true)
                    }else {
                        $("#buke").attr("checked", true)
                    }
                }

                if(data.data.remark!=null){
                    $("#_remark").val(data.data.remark);
                }

                if (data.data.hoursEnrolled != null) {
                    var hour = parseInt(data.data.hoursEnrolled / 60);
                    var min = data.data.hoursEnrolled - hour * 60;
                    $("#_hours").val(hour);
                    $("#_minutes").val(min);
                }

                if (data.data.payMethod != null) {
                    if (data.data.payMethod == "淘宝付款") {
                        $("#taobao").attr("checked", true)
                    } else if (data.data.payMethod == "微信付款") {
                        $("#weixin").attr("checked", true)
                    } else if (data.data.payMethod == "转学生") {
                        $("#zhuan").attr("checked", true)
                    } else if (data.data.payMethod == "恢复上课") {
                        $("#hui").attr("checked", true)
                    } else if (data.data.payMethod == "其他") {
                        $("#qita").attr("checked", true)
                    }
                }

                if (data.data.studentId != null) {
                    var idl = data.data.studentId;
                    select2ByValueStudentId(idl);
                }

                if (data.data.sellerId != null) {
                    var idl = data.data.sellerId;
                    // $("#_sellerId").select2().val(idl).trigger("change");
                    select2ByValue(idl);
                }

                if(data.data.money!=null){
                    $("#_money").val(data.data.money);
                }
                $('select').select2().change();


                // $('select').select2().change();
                // var arr = [];
                // for(var i = 0; i < data.length; i++) {
                //     htm += '<option value="' + data[i].studentId + '">' + data[i].enName + '  ' + data[i].zoomId + '</option>'
                //     var obj = {};
                //     obj.id = data[i].studentId;
                //     obj.text = data[i].enName + '  ' + data[i].zoomId;
                // }
                // $('#_studentId').append(htm);
            },
            error: function() {
                //parent.layer.alert('加载学生信息出错！');
            }
        });
    }

</script>
</body>

</html>
